//
//  NRWSellVC.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/11.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWSellCarVC.h"
#import "NRWSellCarListCell.h"
#import <SDCycleScrollView.h>
#import "UIImage+ImageCompress.h"
#import "NRWSellCarInputInfoVC.h"
#import "LRTranstionAnimationPush.h"
#import "NRWSellCarListApi.h"
#import "NRWSellCarListModel.h"


@interface NRWSellCarVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,UINavigationControllerDelegate>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic,strong) SDCycleScrollView *bannerView;
@property (nonatomic,strong) NRWSellCarListApi *sellCarLisrApi;
@property (nonatomic, strong) NRWSellCarListModel *carListModel;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation NRWSellCarVC
-(NRWSellCarListApi *)sellCarLisrApi{
    if (!_sellCarLisrApi) {
        _sellCarLisrApi = [[NRWSellCarListApi alloc] init];
        _sellCarLisrApi.delegate = self;
    }
    return _sellCarLisrApi;
}
-(NRWSellCarListModel *)carListModel{
    if (!_carListModel) {
        _carListModel = [[NRWSellCarListModel alloc] init];
    }
    return _carListModel;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
#pragma mark - lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationView setTitle:@"卖车"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccessful:) name:userLoginSuccess object:nil];

    [self initTableview];
}
-(void)loginSuccessful:(NSNotification *)nofit{
    if ([nofit.object isEqualToString:kSellCar]) {
        [self pushInputMessage];
        [self.sellCarLisrApi start];
    }

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    if ([NRWUserInfoModel shared].isLogin == YES) {
        [self.sellCarLisrApi start];
    }else{
        [self.dataArray removeAllObjects];
        [self.tableview reloadData];
    }
    self.button.userInteractionEnabled = YES;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
#pragma mark - setupUI
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = backColor();
    [self.tableview registerNib:[UINib nibWithNibName:@"NRWSellCarListCell" bundle:nil] forCellReuseIdentifier:@"NRWSellCarListCell"];
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.tableFooterView = DPView();
//    [self.tableview addHeaderRefreshWithTarget:self action:@selector(loadSellList)];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(getNavHeight());
        make.left.right.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.view).mas_offset(-getTabBarHeight());
    }];
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH(), SCREEN_WIDTH() * 6 / 25) delegate:self placeholderImage:[UIImage imageNamed:@"banner"]];
    self.bannerView.currentPageDotImage = [UIImage imageWithColor:mainColor() forSize:CGSizeMake(15, 2)];
    self.bannerView.pageDotImage = [UIImage imageWithColor:[UIColor whiteColor] forSize:CGSizeMake(15, 2)];
    self.bannerView.localizationImageNamesGroup = @[@"sell_car_banner"];
    self.tableview.tableHeaderView = self.bannerView;
    
    UIButton *subscribeBtn = [[UIButton alloc] init];
    [subscribeBtn setImage:getImage(@"sell_car") forState:UIControlStateNormal];
    [subscribeBtn addTarget:self action:@selector(subscribeBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    subscribeBtn.frame = CGRectMake(SCREEN_WIDTH() - 75 - 12, SCREEN_HEIGHT() - getTabBarHeight() - 75 - 32, 75, 75);
    self.button = subscribeBtn;
    [self.view addSubview:subscribeBtn];
    [self.view bringSubviewToFront:subscribeBtn];
}
#pragma mark - networkDelegate
-(void)loadSellList{
    [self.sellCarLisrApi start];
}
- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    if (request == self.sellCarLisrApi) {
        [self.dataArray removeAllObjects];
        self.carListModel = [NRWSellCarListModel mj_objectWithKeyValues:request.responseObject];
        if (self.carListModel.code == 1) {
            [self.dataArray addObjectsFromArray:self.carListModel.result];
            [self.tableview reloadData];
            
        }else{
            
            [NRWMessageHUD showMessage:request.responseObject[@"msg"] forTime:2.0 withRemindId:remindFail];
        }
        [self.tableview.mj_header endRefreshing];
    }
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    

}
#pragma mark - DelegateMethod
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    carListModel *model = self.dataArray[indexPath.row];
    NRWSellCarListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWSellCarListCell" forIndexPath:indexPath];
    cell.carModel = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    
}
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index{
    
}

#pragma mark - privateMethod

#pragma mark - publicMethod

#pragma mark - touchEventAction
-(void)subscribeBtnDidClick:(UIButton *)sender{
    
    if ([NRWUserInfoModel shared].isLogin == YES) {
        sender.userInteractionEnabled = NO;
        [self pushInputMessage];
    }else{
        NRWLoginView *loginView = [[NRWLoginView alloc] init];
        loginView.type = kSellCar;
        [loginView show];
    }
}

-(void)pushInputMessage{
    NRWSellCarInputInfoVC *inputInfoVC = [[NRWSellCarInputInfoVC alloc] init];
    inputInfoVC.inputCarSuccessful = ^{
    };
    [self.navigationController pushViewController:inputInfoVC animated:YES];
}
#pragma mark - setter

#pragma mark - getter

#pragma mark -- UINavigationControllerDelegate --

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    
    if (operation == UINavigationControllerOperationPush && [toVC isKindOfClass:[NRWSellCarInputInfoVC class]]) {
        return [LRTranstionAnimationPush new];
    }else{
        return nil;
    }
}
@end
